package com.meteor.security.service;

import lombok.AllArgsConstructor;
import com.meteor.framework.security.mobile.MobileUserDetailsService;
import com.meteor.user.convert.SysUserConvert;
import com.meteor.user.mapper.SysUserMapper;
import com.meteor.user.entity.SysUser;
import com.meteor.user.service.SysUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 手机验证码登录 MobileUserDetailsService
 *
 * @author lynn
 * @date 2024/1/03
 */
@Service
@AllArgsConstructor
public class MobileUserDetailsServiceImpl implements MobileUserDetailsService {
    private final SysUserDetailsService sysUserDetailsService;
    private final SysUserMapper sysUserMapper;

    @Override
    public UserDetails loadUserByMobile(String mobile,String communityId) throws UsernameNotFoundException {
        SysUser userEntity = sysUserMapper.getByMobile(mobile,communityId);
        if (userEntity == null) {
            throw new UsernameNotFoundException("手机号或验证码错误");
        }

        return sysUserDetailsService.getUserDetails(SysUserConvert.INSTANCE.convertDetail(userEntity));
    }

}
